<?php
	if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Mmod_tcmanipulation extends MYAD_Model{
        /*
         * declared all table name using
         */
	protected $table_name = 'timecard';
        protected $table_employee = 'm_employee';
        protected $table_leaveregistration = 'm_leaveregistration';
        protected $table_overtime = 'm_overtimeregistration';
        protected $table_holiday = 'm_holiday';
        protected $table_leaveinfo = 'm_employeeleaveinfo';
        protected $table_shiftinfo = 'shiftinfo';
        protected $table_leavetype = 'm_leavetype';
        protected $dateHoliday = 2;
        protected $dateWorking = 1;
        protected $dateUndefined = 0;
        protected $dateHighLight = 1;
        protected $dateUnHighLight = 0;
        // end
        function __construct(){
            parent::__construct();
            // load model employee
            $this->load->model('mod_employee/Mmod_employee');
	}
        /*
         * Main function 
         */
        function allTimeWorkingForEmployeeFromData($shiftInfoBase,$employeeInfo,$arrTime,$ColemployeeID,$Coldate,$Colarrivaltime,$Colleavetime){
            // month caculate salary
            $monthCurrentSalary = '';
            $yearCurrentSalary = '';
            // get month
            if(isset($arrTime[0]) && count($arrTime[0])>0){
                $monthData = $arrTime[0][$Coldate];
            }
            else{
                if(isset($arrTime[1]) && count($arrTime[1])>0){
                    $monthData = $arrTime[1][$Coldate];
                }
            }
            $monthCurrentSalary = date("m",strtotime($monthData));
            $yearCurrentSalary = date("Y",strtotime($monthData));
            // number day of month and year
            $numberDay = cal_days_in_month(1, $monthCurrentSalary, $yearCurrentSalary);
            /*
             * Step 0. Clear all time card
             */
            $this->deleteMonthOfTimecard($employeeInfo,$monthCurrentSalary,$yearCurrentSalary);
            /*
             * Step 1. install all data for month with employee
             */
            $this->instalDataAllDayOfMonth($shiftInfoBase,$numberDay,$employeeInfo,$monthCurrentSalary,$yearCurrentSalary,$arrTime);
            /*
             * end step 1
             */
            /*
             * Step 2. update holiday to timecard
             */
            $this->updateDataHolidayOfMonth($employeeInfo,$monthCurrentSalary,$yearCurrentSalary,$shiftInfoBase,$arrTime);
            /*
             * end step 2
             */
            /*
             * Step 3. update leave registration is paid money
             */
            $this->updateDataLeaveIsPaid($numberDay,$employeeInfo,$monthCurrentSalary,$yearCurrentSalary,$shiftInfoBase,$arrTime);
            /*
             * end step 3
             */
            /*
             * Step 4. update leave special day
             */
            $this->updateDataSpecialDayIsPaid($employeeInfo,$monthCurrentSalary,$yearCurrentSalary,$shiftInfoBase,$arrTime);
            /*
             * end step 4
             */
            $this->db->trans_begin();
            foreach($arrTime as $rowArrTimes){
                // caculate overtime previous date pending
                
                // get employee id from data
                if(isset($rowArrTimes[$ColemployeeID])&& $rowArrTimes[$ColemployeeID]!='' && $this->checkEmployeeCardId($rowArrTimes[$ColemployeeID])){
                    $employee = $this->getEmployeeIDByCardID($rowArrTimes[$ColemployeeID]);                       
                    if(count($employee)> 0){
                        // get employee id
                        $employeeID = $employee['EmployeeId'];
                        //get leave registration for date
                        $registrationDates = $this->getLeaveRegistrationEmployeeID($employeeID,$rowArrTimes[$Coldate]);
                        $dateType = $this->checkDatetype($rowArrTimes[$Coldate]);
                        $data = $this->caculationTimeWorkingNormalWeekDay($rowArrTimes,$shiftInfoBase,$employeeID,$Coldate,$Colarrivaltime,$Colleavetime,$dateType);
                        die;
                        $this->db->where('EmployeeId',$employeeID);
                        $this->db->where('Date',$rowArrTimes[$Coldate]);
                        $this->db->update($this->table_name,$data);
                    }
                }
            }
            if ($this->db->trans_status() === FALSE)
            {
                $this->db->trans_rollback();
                return FALSE;
            }
            else
            {
                $this->db->trans_commit();
                return TRUE;
            }            
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:  install all data for month with employee
         * @Input :         (Array) $arrTime,$employee ($string) $monthCurrentSalary,$yearCurrentSalary
         * @Output :        Null
         */  
        function instalDataAllDayOfMonth($shiftInfoBase,$numberDay,$employeeInfo,$monthCurrentSalary,$yearCurrentSalary,$arrTime){
            $DateType = 0;
            // insert data
            foreach ($employeeInfo as $empRow) {
                if($this->checkExistInArray($empRow['IdCardNumber'],$arrTime)){
                    for($i=1;$i<=$numberDay;$i++){
                        $Date = $yearCurrentSalary."-".$monthCurrentSalary."-".$i;
                        if($this->checkDatetype($Date)==2){
                            $DateType=$this->dateHoliday;
                        }
                        else{
                            $DateType =$this->dateUndefined;
                        }
                        $WeekDay = date('D', strtotime($Date));
                        $data = array(
                            'EmployeeId'                =>  (integer)$empRow['EmployeeId'],
                            'ShiftId'                   =>  $shiftInfoBase['ShiftId'],
                            'Date'                      =>  $Date,
                            'DateType'                  =>  $DateType,
                            'ActualArrivalTime'         =>  '00:00:00',
                            'ActualLeaveTime'           =>  '00:00:00',
                            'TrimmedArrivalTime'        =>  '00:00:00',
                            'TrimmedLeaveTime'          =>  '00:00:00',
                            'ResTime1'                  =>  0,
                            'ResTime2'                  =>  0,
                            'ResTime3'                  =>  0,
                            'WeekdayNormalTime'         =>  0,
                            'WeekdayOvertime'           =>  0,
                            'WeekdayLateOvertime'       =>  0,
                            'WeekendOvertime'           =>  0,
                            'WeekendLateOvertime'       =>  0,
                            'HolidayOvertime'           =>  0,
                            'HolidayLateOvertime'       =>  0,
                            'LateArrival'               =>  0,
                            'EarlyLeave'                =>  0,
                            'WeekDay'                   =>  $WeekDay,
                            'LeaveIsPaid'               =>  0,
                            'LeaveNoPaid'               =>  0,
                            'ActualWorkHours'           =>  0,                          
                            'TimeWorking'               =>  0,                          
                            'HighLight'                 =>  $this->dateUnHighLight,                         
                            'Ins_ID'                    =>  $this->session->userdata('UserId'),                         
                            'Ins_Date'                  =>  date('Y-m-d H:i:s'),                         
                        );
                        $this->db->insert($this->table_name, $data); 
                    }
                }
            }
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:  update holiday with month
         * @Input :         (Array) $employee ($string) $monthCurrentSalary,$yearCurrentSalary
         * @Output :        Null
         */
        function updateDataHolidayOfMonth($employeeInfo,$monthCurrentSalary,$yearCurrentSalary,$shiftInfoBase,$arrTime){
            foreach ($employeeInfo as $empRow) {
                // get all holiday of employee with month input
                $holidayOfEmployee = $this->getHolidayOfMonth($monthCurrentSalary,$yearCurrentSalary);
                // check exist holiday
                if(count($holidayOfEmployee)>0){
                    foreach ($holidayOfEmployee as $holiRow) {
                        if($this->checkExistInArray($empRow['IdCardNumber'],$arrTime)){
                            $dayWorking = date("w", strtotime($holiRow['Date']));
                            $data = array(
                                'DateType'                  =>  $this->dateWorking,
                                'TrimmedArrivalTime'        =>  $shiftInfoBase['StartTime'],
                                'TrimmedLeaveTime'          =>  $shiftInfoBase['EndTime'],
                                'WeekdayNormalTime'         =>  $shiftInfoBase['TotalWorkHours'],
                                'ActualWorkHours'           =>  $shiftInfoBase['TotalWorkHours'],
                                'TimeWorking'               =>  $shiftInfoBase['TotalWorkHours'],                          
                                'HighLight'                 =>  $this->dateUnHighLight,                                 
                            );
                            $this->db->where('Date',$holiRow['Date']);
                            $this->db->where('EmployeeId',$empRow['EmployeeId']);
                            $this->db->update($this->table_name, $data);
                        }
                    }
                }
            }
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    update Leave registraion is paid
         * @Input :         (Array) $employee ($string) $monthCurrentSalary,$yearCurrentSalary
         * @Output :        Null
         */
        function updateDataLeaveIsPaid($numberDay,$employeeInfo,$monthCurrentSalary,$yearCurrentSalary,$shiftInfoBase,$arrTime){
            $hourOffWork = 0;
            // load all employee
            foreach ($employeeInfo as $empRow) {
                if($this->checkExistInArray($empRow['IdCardNumber'],$arrTime)){
                    // get all leave registration is paid of employee
                    $leaveIsPaid = $this->getLeaveRegistrationIsPaid($monthCurrentSalary,$yearCurrentSalary,$empRow['EmployeeId']);
                    // check exist data
                    if(count($leaveIsPaid)>0){
                        foreach ($leaveIsPaid as $rowLeave) {
                            for($i=1;$i<=$numberDay;$i++){
                                $date = $yearCurrentSalary."-".$monthCurrentSalary."-".$i;
                                $dayWorking = date("w", strtotime($date));
                                if($dayWorking != 0 && $dayWorking != 6){
                                    // time start time
                                    $startTime = strtotime($date." ".$shiftInfoBase['StartTime']);
                                    // time end time
                                    $endTime = strtotime($date." ".$shiftInfoBase['EndTime']);
                                    // leave registraion start time
                                    $startLeaveTime = strtotime($rowLeave['StartDate']." ".$rowLeave['StartTime']);
                                    // leave registraion end time
                                    $endLeaveTime = strtotime($rowLeave['EndDate']." ".$rowLeave['EndTime']);
                                    
                                    // case 1  startTime < startLeaveTime <= endTime < endLeavetime
                                    if(($startTime < $startLeaveTime) && ($startLeaveTime <= $endTime) && ($endTime < $endLeaveTime) ){
                                        // the time off work
                                        $hourOffWork = $this->theTimeOffWork($shiftInfoBase,$rowLeave['StartTime'],$shiftInfoBase['EndTime']);
                                        $data = array(
                                            'DateType'                  =>  $this->dateWorking,
                                            'TrimmedArrivalTime'        =>  $shiftInfoBase['StartTime'],
                                            'TrimmedLeaveTime'          =>  $shiftInfoBase['EndTime'],
                                            'WeekdayNormalTime'         =>  $hourOffWork,
                                            'ActualWorkHours'           =>  $hourOffWork,
                                            'TimeWorking'               =>  $hourOffWork,                          
                                            'HighLight'                 =>  $this->dateUnHighLight,                                            
                                            
                                        );
                                        $this->db->where('Date',$date);
                                        $this->db->where('EmployeeId',$empRow['EmployeeId']);
                                        $this->db->update($this->table_name, $data);
                                    }
                                    // case 2 startLeaveTime <= startTime  and endTime <= endLeavetime
                                    if(($startLeaveTime <= $startTime) && ($endLeaveTime >= $endTime) ){
                                        // the time off work special full day leave registration
                                        $hourOffWork = $shiftInfoBase['TotalWorkHours'];
                                        $data = array(
                                            'DateType'                  =>  $this->dateWorking,
                                            'TrimmedArrivalTime'        =>  $shiftInfoBase['StartTime'],
                                            'TrimmedLeaveTime'          =>  $shiftInfoBase['EndTime'],
                                            'WeekdayNormalTime'         =>  $hourOffWork,
                                            'ActualWorkHours'           =>  $hourOffWork,
                                            'TimeWorking'               =>  $hourOffWork,                          
                                            'HighLight'                 =>  $this->dateUnHighLight,                                            
                                        );
                                        $this->db->where('Date',$date);
                                        $this->db->where('EmployeeId',$empRow['EmployeeId']);
                                        $this->db->update($this->table_name, $data);
                                    }
                                    // case 3 startLeaveTime < startTime <= endLeavetime
                                    if( ($startLeaveTime < $startTime) && ($startTime <= $endLeaveTime) && ($endLeaveTime < $endTime) ){
                                        // the time off work
                                        $hourOffWork = $this->theTimeOffWork($shiftInfoBase,$shiftInfoBase['StartTime'],$rowLeave['EndTime']);
                                        $data = array(
                                            'DateType'                  =>  $this->dateWorking,
                                            'TrimmedArrivalTime'        =>  $shiftInfoBase['StartTime'],
                                            'TrimmedLeaveTime'          =>  $shiftInfoBase['EndTime'],
                                            'WeekdayNormalTime'         =>  $hourOffWork,
                                            'ActualWorkHours'           =>  $hourOffWork,
                                            'TimeWorking'               =>  $hourOffWork,                          
                                            'HighLight'                 =>  $this->dateUnHighLight,                                            
                                        );
                                        $this->db->where('Date',$date);
                                        $this->db->where('EmployeeId',$empRow['EmployeeId']);
                                        $this->db->update($this->table_name, $data);
                                    }
                                    // case 4  startTime <= startLeaveTime <= $endLeaveTime<=endTime
                                    if( ($startTime <= $startLeaveTime) && ($startLeaveTime <= $endTime) && ($endLeaveTime >= $startTime) && ($endLeaveTime <= $endTime) ){
                                        // the time off work
                                        $hourOffWork = $this->theTimeOffWork($shiftInfoBase,$rowLeave['StartTime'],$rowLeave['EndTime']);
                                        $data = array(
                                            'DateType'                  =>  $this->dateWorking,
                                            'TrimmedArrivalTime'        =>  $shiftInfoBase['StartTime'],
                                            'TrimmedLeaveTime'          =>  $shiftInfoBase['EndTime'],
                                            'WeekdayNormalTime'         =>  $hourOffWork,
                                            'ActualWorkHours'           =>  $hourOffWork,
                                            'TimeWorking'               =>  $hourOffWork,                          
                                            'HighLight'                 =>  $this->dateUnHighLight,                                            
                                        );
                                        $this->db->where('Date',$date);
                                        $this->db->where('EmployeeId',$empRow['EmployeeId']);
                                        $this->db->update($this->table_name, $data);
                                    }                                   
                                }
                            }
                        }
                    }
                }
            }
        }
        /*
         * @author :        HungLV
         * @Description:    check exist employee id 
         * @Input :         (Number) $id
         * @Output :        Return true or false
         */

        function checkEmployeeCardId($carId) {
            $this->db->where('IdCardNumber', $carId);
            $this->db->where('CompanyCode', $this->session->userdata('CompanyCode'));
            $query = $this->db->get($this->table_employee);
            if ($query->num_rows() > 0) {
                return TRUE;
            } else {
                return FALSE;
            }
        }        
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    check Id Number exist in array
         * @Input :         (Array) $arrTime (int) $employeeID
         * @Output :        return true or false
         */ 
        function checkExistInArray($employeeID,$arrTime){            
            foreach($arrTime as $rowArrTime){
                if($rowArrTime[1]==$employeeID){
                    return TRUE;
                }
            }
            return FALSE;
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    update Specail Leave Is Paid
         * @Input :         (Array) $employee ($string) $monthCurrentSalary,$yearCurrentSalary
         * @Output :        Null
         */        
        function updateDataSpecialDayIsPaid($employeeInfo,$monthCurrentSalary,$yearCurrentSalary,$shiftInfoBase,$arrTime){
            foreach ($employeeInfo as $empRow) {
                // check IdCardNumber exist
                if($this->checkExistInArray($empRow['IdCardNumber'],$arrTime)){
                    $specialDay = $this->getSpecialDay($empRow['EmployeeId'],$monthCurrentSalary,$yearCurrentSalary);
                    if(count($specialDay)>0){
                        $data = array(
                            'DateType'                  =>  1,
                            'TrimmedArrivalTime'        =>  $shiftInfoBase['StartTime'],
                            'TrimmedLeaveTime'          =>  $shiftInfoBase['EndTime'],
                            'WeekdayNormalTime'         =>  $shiftInfoBase['TotalWorkHours'],
                            'ActualWorkHours'           =>  $shiftInfoBase['TotalWorkHours'],
                            'TimeWorking'               =>  $shiftInfoBase['TotalWorkHours'],                       
                            'HighLight'                 =>  1,                             
                        );
                        $this->db->where('Date',$specialDay['SpecicalDate']);
                        $this->db->where('EmployeeId',$empRow['EmployeeId']);
                        $this->db->update($this->table_name, $data);                         
                    }
                }
            }
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    get the time off work into leave registraion type is paid
         * @Input :         (Array) $shiftInfoBase (Time) $timeStart,$timeEnd
         * @Output :        (float) hour the off work
         */      
        function theTimeOffWork($shiftInfoBase,$timeStart,$timeEnd){
            // case 1
            if( (strtotime($timeStart)>=strtotime($shiftInfoBase['StartTime'])) && ( strtotime($timeStart)<=strtotime($shiftInfoBase['StartRestTime1']) ) && ( strtotime($timeEnd)>=strtotime($shiftInfoBase['EndRestTime1']) ) && ( strtotime($timeEnd)<=strtotime($shiftInfoBase['EndTime']) ) ){
                return $this->hourTwoInput($shiftInfoBase['StartRestTime1'],$timeStart,$timeEnd,$shiftInfoBase['EndRestTime1']);
                
            }
            // case 2
            if( ( strtotime($timeStart)>=strtotime($shiftInfoBase['StartRestTime1']) ) && ( strtotime($timeStart)>=strtotime($shiftInfoBase['EndRestTime1']) ) && ( strtotime($timeEnd)>=strtotime($shiftInfoBase['EndRestTime1']) ) && ( strtotime($timeEnd)<=strtotime($shiftInfoBase['EndTime']) ) ){
                return $this->hourOneInput($timeEnd,$shiftInfoBase['EndRestTime1']);
                
            }
            // case 3
            if( ( strtotime($timeStart)>=strtotime($shiftInfoBase['StartTime']) ) && ( strtotime($timeStart)<=strtotime($shiftInfoBase['StartRestTime1']) ) && ( strtotime($timeEnd)>=strtotime($shiftInfoBase['StartRestTime1']) ) && ( strtotime($timeEnd)<=strtotime($shiftInfoBase['EndRestTime1']) ) ){
                return $this->hourOneInput($shiftInfoBase['StartRestTime1'],$timeStart);
                
            }
            // case 4 special
            if( ( strtotime($timeStart)>=strtotime($shiftInfoBase['StartRestTime1']) ) && ( strtotime($timeStart)<=strtotime($shiftInfoBase['EndRestTime1']) ) && ( strtotime($timeEnd)>=strtotime($shiftInfoBase['StartRestTime1']) ) && ( strtotime($timeEnd)<=strtotime($shiftInfoBase['EndRestTime1']) ) ){
                return 0;
            }
            // case 5
            if( ( strtotime($timeStart)>=strtotime($shiftInfoBase['StartTime']) ) && ( strtotime($timeStart)<=strtotime($shiftInfoBase['StartRestTime1']) ) && ( strtotime($timeEnd)>=strtotime($shiftInfoBase['StartTime']) ) && ( strtotime($timeEnd)<=strtotime($shiftInfoBase['StartRestTime1']) ) ){                
                return $this->hourOneInput($timeEnd,$timeStart);
                
            }
            // case 6
            if( ( strtotime($timeStart)>=strtotime($shiftInfoBase['EndRestTime1']) ) && ( strtotime($timeStart)<=strtotime($shiftInfoBase['EndTime']) ) && ( strtotime($timeEnd)>=strtotime($shiftInfoBase['EndRestTime1']) ) && ( strtotime($timeEnd)<=strtotime($shiftInfoBase['EndTime']) ) ){                
                return $this->hourOneInput($timeEnd,$timeStart);
            }
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    get special day
         * @Input :         (Int) $employeeID,$month,$year
         * @Output :        (Array) Return Array data
         */
        function getSpecialDay($employeeID,$month,$year){
            $this->db->from($this->table_leaveinfo.' as a ');
            $this->db->where("MONTH(a.SpecicalDate)=", $month);
            $this->db->where("a.Year",$year);        
            $this->db->where('a.Del_Flag',0);      
            $this->db->where('a.EmployeeId',$employeeID);
            $query = $this->db->get();
            return $query->row_array();
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    get all Holiday Of current Month and current Year
         * @Input :         (Int) $month,$year
         * @Output :        (Array) Return Array data
         */         
        function getHolidayOfMonth($month,$year){
            $this->db->from($this->table_holiday.' as a ');
            $this->db->where("MONTH(a.Date)=", $month);
            $this->db->where("YEAR(a.Date)=", $year);        
            $this->db->where("a.Del_Flag",0);        
            $this->db->where("a.CompanyCode",$this->session->userdata('CompanyCode'));         
            $query = $this->db->get();
            return $query->result_array(); 
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    get all leave registration is paid current Month and current Year with employeeid
         * @Input :         (Int) $month,$year,$employeeID
         * @Output :        (Array) Return Array data
         */        
        function getLeaveRegistrationIsPaid($month,$year,$employeeID){
            $this->db->from($this->table_leaveregistration.' as a ');
            $this->db->join($this->table_leavetype.' as b ','b.LeaveTypeId = a.LeaveTypeId');
            $this->db->where('b.IsPaidLeave',1);
            $this->db->where('a.EmployeeId', $employeeID);
            $this->db->where("MONTH(a.StartDate)=", $month);
            $this->db->where("YEAR(a.StartDate)=", $year);
            $this->db->where('a.Del_Flag',0);
            $query = $this->db->get();
            return $query->result_array(); 
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:  Clear all time card with month
         * @Input :         (Array) $employee ($string) $monthCurrentSalary,$yearCurrentSalary
         * @Output :        Null
         */        
        function deleteMonthOfTimecard($employeeInfo,$month,$year){
            foreach ($employeeInfo as $empRow) {
                $this->db->where("MONTH(timecard.Date)=", $month);
                $this->db->where("EmployeeId", $empRow['EmployeeId']);
                $this->db->where("YEAR(timecard.Date)=", $year);
                $this->db->delete($this->table_name);                
            }
        }
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    caculate time registration no paid of current date
         * @Input :         (Array) $employee ($string) $monthCurrentSalary,$yearCurrentSalary
         * @Output :        Null
         */
        function getRegLeaveNoIsPaid($employeeID,$dateCurrent){
            $this->db->from($this->table_leaveregistration.' as a ');
            $this->db->join($this->table_leavetype.' as b ', 'b.LeaveTypeId  = a.LeaveTypeId');
            $this->db->where('a.StartDate',$dateCurrent);
            $this->db->where('a.EmployeeId',$employeeID);
            $this->db->where('b.IsPaidLeave',0);
            $query = $this->db->get();
            return $query->result_array();
        }
        function deleteDateOfTimecard($date){
            $this->db->where('Date',$date);
            $this->db->delete($this->table_name);
        }
        function insertDataTimeCard($data){
            $this->db->insert($this->table_name, $data);
        }     
        /**
         * function return num row overtime with employee id and date
         * parameter: $employeeID,$date
         */           
        function isCheckOvertime($employeeID,$date){
            $this->db->from($this->table_overtime);
            $this->db->where('EmployeeId',$employeeID);
            $this->db->where('StartDate',$date);
            $this->db->where('Del_Flag',0);
            $query = $this->db->get();
            return $query->row_array();
        }
        function isCheckLeaveRegIsPaid($employeeID,$currentDate){
            $this->db->from($this->table_leaveregistration.' as a ');
            $this->db->join($this->table_leavetype.' as b ', 'b.LeaveTypeId  = a.LeaveTypeId');            
            $this->db->where('a.EmployeeId',$employeeID);
            $this->db->where('a.StartDate', $currentDate);
            $this->db->or_where('a.EndDate', $currentDate);
            $this->db->where('b.IsPaidLeave',1);
            $this->db->where('a.Del_Flag',0);
            $query = $this->db->get();
            if($query->num_rows() > 0){
                return TRUE;
            }
            else{
                return FALSE;
            }
        }
        function timeLeavePaidMoney($employeeID,$currentDate){
            $this->db->from($this->table_name);
            $this->db->where('EmployeeId',$employeeID);
            $this->db->where('Date',$currentDate);
            $query = $this->db->get();
            if($query->num_rows() > 0){
                $result = $query->row_array();
                return $result['WeekdayNormalTime'];
            }
            else{
                return 0;
            }            
        }
        /**
         * function getEmployeeIDByCardID
         * get Info Employee by ID of employee
         * parameter: $cardid (Card ID)
         */           
        function getEmployeeIDByCardID($cardid){
            $this->db->from($this->table_employee.' as a ');
            $this->db->where('a.IdCardNumber',$cardid);
            $this->db->where('a.CompanyCode',$this->session->userdata('CompanyCode'));
            $this->db->where('a.Status',1);
            $this->db->where('a.Del_Flag',0);
            $query = $this->db->get();
            return $query->row_array(); 
        } 
        /**
         * function getLeaveRegistrationEmployeeID
         * get Leave Registration of Employee with id of employee
         * parameter: $employeeID, $date
         */        
        function getLeaveRegistrationEmployeeID($employeeID,$date){
            $this->db->from($this->table_leaveregistration.' as a ');
            $this->db->where('a.EmployeeId',$employeeID);
            $this->db->where('a.StartDate <= ', $date);
            $this->db->where('a.EndDate >= ', $date);
            $this->db->where('a.Del_Flag',0);
            $query = $this->db->get();
            return $query->result_array();
        }
        function reUpdateTimeCardOvertime($nameUpdate,$hourUpdate,$employeeID,$Prevdate,$dbActualWorkHours){
            $data = array(
                $nameUpdate => $hourUpdate,
                'ActualWorkHours'=> $dbActualWorkHours
            );
            $this->db->where('EmployeeId',$employeeID);
            $this->db->where('Date',$Prevdate);
            $this->db->update($this->table_name,$data);
        }
        function getActualWorkHoursPrevDate($employeeID,$Prevdate){            
            $this->db->where('EmployeeId',$employeeID);
            $this->db->where('Date',$Prevdate);
            $query = $this->db->get($this->table_name);
            return $query->row_array();
        }        
        /*
         * @author :        HungLV
         * @created:        
         * @Description:    caculate time working actual
         * @Input :         (Array) $arrTime,$shiftInfoBase,$shiftInfoS (Orther) $employeeID,$Coldate,$Colarrivaltime,$Colleavetime,$rowdata
         * @Output :        return array data
         */
        function caculationTimeWorkingNormalWeekDay($rowArrTimes,$shiftInfoBase,$employeeID,$Coldate,$Colarrivaltime,$Colleavetime,$dateWokingType){
            /*
             * Update overtime for previous date
             */
            // check previous have OT and Time OT current date
            $prevDate = date('Y-m-d',strtotime($rowArrTimes[$Coldate] .' -1 day'));
            $overtimePrevDate = $this->isCheckOvertime($employeeID,$prevDate);
            print_r($overtimePrevDate);
            if(count($overtimePrevDate)>0){
                $this->updateOvetTimeForPrevDate($employeeID,$rowArrTimes[$Colarrivaltime],$prevDate,$rowArrTimes[$Coldate],$overtimePrevDate,$shiftInfoBase['EndTime'],$shiftInfoBase['StartTime']);
            }
            /*
             * end update overtime for previous date
             */
            // get month
            $monthCurrent = date("m",strtotime($rowArrTimes[$Coldate]));
            // get year
            $yearCurrent = date("Y",strtotime($rowArrTimes[$Coldate]));            
            // Trimmed Arrival Time
            $Arrivaltime = $this->trimmedArrivaltime($rowArrTimes[$Colarrivaltime],$shiftInfoBase['RoundedTimeBlock'],$shiftInfoBase['StartTime']);
            // get $WeekDay as: Friday
            $WeekDay = date('D', strtotime($rowArrTimes[$Coldate]));
            // date type 1: as normal time                        
            $dateType = 1;
            // get leave time no is paid
            $RegLeaveNoPaid = $this->getRegLeaveNoIsPaid($employeeID,$rowArrTimes[$Coldate]);
            /*
             * install data the initial value
             */
            // time working week day late over time
            $WeekdayLateOvertime = 0;
            // time working week day  over time
            $WeekdayOvertime = 0;
            // time working week day normal time
            $WeekdayNormalTime = 0;
            // time working weekend over time
            $WeekendOvertime = 0;
            // time working weekend late over time
            $WeekendLateOvertime = 0;
            // time working holiday over time
            $HolidayOvertime = 0;
            // time working holiday late over time
            $HolidayLateOvertime = 0;                                                                                                                                                
            // time restime 1
            $ResTime1 = 0;     
            // time restime 2
            $ResTime2 = 0; 
            // time restime 3
            $ResTime3 = 0;
            // time leave registration is paid
            $LeaveIsPaid = 0;
            // time leave registration no paid
            $LeaveNoPaid = 0;
            // Time Late Arrival
            $LateArrival = 0;
            // Time Early Leave
            $EarlyLeave = 0;                        
            // Time Actual WorkHours
            $ActualWorkHours = 0;
            // high light 
            $HighLight = 0;
            // Time leave no paid
            $Restime30 = 0;
            $Restime20 = 0;
            $Restime10 = 0;
        // caculation time working day
            // if arrivaltime != empty
            if($rowArrTimes[$Colleavetime]!=""){
                // Timmed Leave Time
                $Leavetime = $this->trimmedLeavetime($rowArrTimes[$Colleavetime],$shiftInfoBase['RoundedTimeBlock']);
                // check $Leavetime > $Arrivaltime
                if( strtotime($Leavetime) > strtotime($Arrivaltime) ){
                    $overTimeCurrentDate = $this->isCheckOvertime($employeeID,$rowArrTimes[$Coldate]);
                    // working overtime
                    if(count($overTimeCurrentDate)>0)
                    {
                        // caculation late over time
                        $WeekdayLateOvertime = $this->weekdayLateOvertime($rowArrTimes[$Coldate],$Arrivaltime,$Leavetime,$shiftInfoBase['EndEveningOvertime'],$shiftInfoBase['RestTime3'],$overTimeCurrentDate);
                        // caculation overtime
                        $WeekdayOvertime = $this->weekdayOvertime($rowArrTimes[$Coldate],$Arrivaltime,$Leavetime,$shiftInfoBase['EndEveningOvertime'],$shiftInfoBase['RestTime2'],$shiftInfoBase['EndTime'],$overTimeCurrentDate);
                        // caculation working nomarl time
                        $WeekdayNormalTime = $this->weekdayNormalTime($Arrivaltime,$Leavetime,$shiftInfoBase['EndTime'],$shiftInfoBase['StartTime'],$shiftInfoBase['EndRestTime1'],$shiftInfoBase['StartRestTime1']);
                        if(count($RegLeaveNoPaid)>0){
                            foreach($RegLeaveNoPaid as $rowLeaveNoPaid){
                                // caculation resetime 30
                                $Restime30 = $Restime30 + $this->resTime30($Arrivaltime,$Leavetime,$shiftInfoBase['EndEveningOvertime'],$rowLeaveNoPaid['StartTime'],$rowLeaveNoPaid['EndTime'],$shiftInfoBase['RestTime3']);
                                // caculation resetime 20
                                $Restime20 = $Restime20 + $this->resTime20($Arrivaltime,$Leavetime,$shiftInfoBase['EndTime'],$shiftInfoBase['RestTime2'],$shiftInfoBase['EndEveningOvertime'],$rowLeaveNoPaid['StartTime'],$rowLeaveNoPaid['EndTime']);
                                // caculation resetime 10
                                $Restime10 = $Restime10 + $this->resTime10($Arrivaltime,$Leavetime,$shiftInfoBase['StartRestTime1'],$shiftInfoBase['EndRestTime1'],$shiftInfoBase['StartTime'],$shiftInfoBase['EndTime'],$rowLeaveNoPaid['StartTime'],$rowLeaveNoPaid['EndTime']);
                            }                                                     
                        }
                        // caculation resetime 3 default
                        $Restime31 = $this->resTime31($shiftInfoBase['RestTime3']);
                        // caculation resetime 2 default
                        $Restime21 = $this->resTime21($shiftInfoBase['RestTime2']);
                        // caculation resetime 1 default
                        $Restime11 = $this->resTime11($Arrivaltime,$Leavetime,$shiftInfoBase['StartRestTime1'],$shiftInfoBase['EndRestTime1']);
                        // caculation $WeekdayLateOvertime
                        $WeekdayLateOvertime = $WeekdayLateOvertime - $Restime30;
                        // caculation $WeekdayOvertime
                        $WeekdayOvertime = $WeekdayOvertime - $Restime20;
                        // caculation $WeekdayNormalTime
                        $WeekdayNormalTime = $WeekdayNormalTime - $Restime10;
                        // caculation time leave is paid
                        if($this->isCheckLeaveRegIsPaid($employeeID,$rowArrTimes[$Coldate])){
                            $timeLeavePaidMoney = $this->timeLeavePaidMoney($employeeID,$rowArrTimes[$Coldate]);
                            $WeekdayNormalTime = $WeekdayNormalTime + $timeLeavePaidMoney;
                        }
                        // Restime 1
                        $ResTime1 = $Restime11 + $Restime10;     
                        // Restime 2
                        $ResTime2 = $Restime21 + $Restime20; 
                        // Restime 3
                        $ResTime3 = $Restime31 + $Restime30;
                        // Late Arrival
                        $LateArrival = $this->caculationLateArrival($Arrivaltime, $shiftInfoBase['StartTime'], $rowArrTimes[$Colarrivaltime]);
                        // Early Leave
                        $EarlyLeave = $this->caculationEarlyLeave($Leavetime, $shiftInfoBase['EndTime'], $rowArrTimes[$Colleavetime]);                           
                        // Actual WorkHours
                        $ActualWorkHours = $WeekdayNormalTime + $WeekdayOvertime + $WeekdayLateOvertime;
                    }
                    // no working overtime
                    else{
                        // caculation working nomarl day time
                        $WeekdayNormalTime = $this->weekdayNormalTime($Arrivaltime,$Leavetime,$shiftInfoBase['EndTime'],$shiftInfoBase['StartTime'],$shiftInfoBase['EndRestTime1'],$shiftInfoBase['StartRestTime1']); 
                        // caculation resetime 1 default
                        $Restime11 = $this->resTime11($Arrivaltime,$Leavetime,$shiftInfoBase['StartRestTime1'],$shiftInfoBase['EndRestTime1']);
                        // caculate time registraiotn leave no paid of current date
                        if(count($RegLeaveNoPaid)>0){
                            foreach($RegLeaveNoPaid as $rowLeaveNoPaid){
                                // caculation resetime leave no paid
                                $Restime10 = $Restime10 + $this->resTime10($Arrivaltime,$Leavetime,$shiftInfoBase['StartRestTime1'],$shiftInfoBase['EndRestTime1'],$shiftInfoBase['StartTime'],$shiftInfoBase['EndTime'],$rowLeaveNoPaid['StartTime'],$rowLeaveNoPaid['EndTime']);
                             }                                                    
                        }                                               
                        // caculation $WeekdayNormalTime
                        $WeekdayNormalTime = ($WeekdayNormalTime - $Restime10);
                        // caculation time leave is paid
                        if($this->isCheckLeaveRegIsPaid($employeeID,$rowArrTimes[$Coldate])){
                            $timeLeavePaidMoney = $this->timeLeavePaidMoney($employeeID,$rowArrTimes[$Coldate]);
                            $WeekdayNormalTime = $WeekdayNormalTime + $timeLeavePaidMoney;
                        }                        
                        // Restime 1
                        $ResTime1 = $Restime11 + $Restime10;
                        // Late Arrival                        
                        $LateArrival = $this->caculationLateArrival($Arrivaltime,$shiftInfoBase['StartTime'],$rowArrTimes[$Colarrivaltime]);
                        // Early Leave
                        $EarlyLeave = $this->caculationEarlyLeave($Leavetime,$shiftInfoBase['EndTime'], $rowArrTimes[$Colleavetime]);                        
                        // Actual WorkHours
                        $ActualWorkHours = $WeekdayNormalTime;
                    }
                }
            }
            if($this->isCheckOvertime($employeeID,$rowArrTimes[$Coldate])){
                $Leavetime = $Leavetime;
            }
            else{
                if(strtotime($Leavetime) > strtotime($shiftInfoBase['EndTime']) ){
                    $Leavetime = $shiftInfoBase['EndTime'];
                }
                else{
                    $Leavetime = $Leavetime;
                }
            }
            // if day working is saturday or sunday
            if( $this->checkDatetype($rowArrTimes[$Coldate])==2 || $this->checkDatetype($rowArrTimes[$Coldate])==3 ){
                
                $dateType = $this->dateUndefined;
                $WeekdayNormalTime = 0;
                $WeekdayOvertime = 0;
                $WeekdayLateOvertime = 0;
                $WeekendOvertime = 0;
                $WeekendLateOvertime = 0;
                $HolidayOvertime = 0;
                $HolidayLateOvertime = 0;
                $ActualWorkHours = 0;
                $HighLight = $this->dateHighLight;
            }
            // special day
            $specialDay = $this->getSpecialDay($employeeID,$monthCurrent,$yearCurrent);
            if(count($specialDay)>0){
                if(strtotime($specialDay['SpecicalDate']) == strtotime($rowArrTimes[$Coldate])){
                    $dateType = $this->dateUndefined;
                    $WeekdayNormalTime = 0;
                    $WeekdayOvertime = 0;
                    $WeekdayLateOvertime = 0;
                    $WeekendOvertime = 0;
                    $WeekendLateOvertime = 0;
                    $HolidayOvertime = 0;
                    $HolidayLateOvertime = 0;
                    $ActualWorkHours = 0;
                    $HighLight = $this->dateHighLight;                   
                }
            }
            // leave time have data full day
            $leaveDayIsPaid = $this->getLeaveRegistrationIsPaid($monthCurrent,$yearCurrent,$employeeID);
            if(count($leaveDayIsPaid)>0){
                foreach ($leaveDayIsPaid as $rowLeaveTime) {
                    // time start time
                    $startTime = strtotime($rowArrTimes[$Coldate]." ".$shiftInfoBase['StartTime']);
                    // time end time
                    $endTime = strtotime($rowArrTimes[$Coldate]." ".$shiftInfoBase['EndTime']);
                    // leave registraion start time
                    $startLeaveTime = strtotime($rowLeaveTime['StartDate']." ".$rowLeaveTime['StartTime']);
                    // leave registraion end time
                    $endLeaveTime = strtotime($rowLeaveTime['EndDate']." ".$rowLeaveTime['EndTime']);                 
                    if(($startLeaveTime<=$startTime) && ( $endLeaveTime>= $endTime )){
                        $dateType = $this->dateUndefined;
                        $WeekdayNormalTime = 0;
                        $WeekdayOvertime = 0;
                        $WeekdayLateOvertime = 0;
                        $WeekendOvertime = 0;
                        $WeekendLateOvertime = 0;
                        $HolidayOvertime = 0;
                        $HolidayLateOvertime = 0;
                        $ActualWorkHours = 0;
                        $HighLight = $this->dateHighLight;               
                    }                    
                }
            }
            $data = array(
                'EmployeeId'                =>  $employeeID,
                'ShiftId'                   =>  $shiftInfoBase['ShiftId'],
                'Date'                      =>  $rowArrTimes[$Coldate],
                'DateType'                  =>  $dateType,
                'ActualArrivalTime'         =>  $rowArrTimes[$Colarrivaltime],
                'ActualLeaveTime'           =>  $rowArrTimes[$Colleavetime],
                'TrimmedArrivalTime'        =>  $Arrivaltime,
                'TrimmedLeaveTime'          =>  $Leavetime,
                'ResTime1'                  =>  $ResTime1,
                'ResTime2'                  =>  $ResTime2,
                'ResTime3'                  =>  $ResTime3,
                'WeekdayNormalTime'         =>  $WeekdayNormalTime,
                'WeekdayOvertime'           =>  $WeekdayOvertime,
                'WeekdayLateOvertime'       =>  $WeekdayLateOvertime,
                'WeekendOvertime'           =>  $WeekendOvertime,
                'WeekendLateOvertime'       =>  $WeekendLateOvertime,
                'HolidayOvertime'           =>  $HolidayOvertime,
                'HolidayLateOvertime'       =>  $HolidayLateOvertime,
                'LateArrival'               =>  $LateArrival,
                'EarlyLeave'                =>  $EarlyLeave,
                'WeekDay'                   =>  $WeekDay,
                'LeaveIsPaid'               =>  $LeaveIsPaid,
                'LeaveNoPaid'               =>  $LeaveNoPaid,
                'ActualWorkHours'           =>  $ActualWorkHours,                                                                                                                             
                'TimeWorking'               =>  $WeekdayNormalTime,                                                                                                                            
                'HighLight'                 =>  $HighLight,                                                                                                                            
            );
            return $data;
        }
        /*
         * 
         */
        function updateOvetTimeForPrevDate($employeeID,$actualArrivalTime,$prevDate,$currentDate,$overtimePrevDate,$endTime,$startTime){
            $hour = 0;
            // begin current day
            $beginTime = "00:00:01";
            // Time begin day
            $startBegin = strtotime($currentDate." ".$beginTime);
            // time start time
            $startTime = strtotime($currentDate." ".$startTime);
            // time end time
            $endTime = strtotime($currentDate." ".$endTime);
            // overtime start time
            $startOverTime = strtotime($overtimePrevDate['StartDate']." ".$overtimePrevDate['StartTime']);
            // overtime end time
            $endOverTime = strtotime($overtimePrevDate['EndDate']." ".$overtimePrevDate['EndTime']);
            if(($endOverTime <= $startTime) && ($endOverTime>=$startBegin)){
                if(strtotime($actualArrivalTime) >= strtotime($overtimePrevDate['EndTime']) ){
                    $hour = $this->hourOneInput($overtimePrevDate['EndTime'], $beginTime);
                }
                else{
                    $hour = $this->hourOneInput($actualArrivalTime, $beginTime);
                }
            }
            // get and set data previous date
            $this->db->where('EmployeeId',$employeeID);
            $this->db->where('Date',$prevDate);
            $query = $this->db->get($this->table_name);
            $result = $query->row_array();
            $newOvertimePrev = $result['WeekdayOvertime']+$hour;
            $newActualWorkHours = $result['ActualWorkHours']+$hour;
            //update data previous date
            $this->db->where('EmployeeId',$employeeID);
            $this->db->where('Date',$prevDate);
            $data = array(
                'WeekdayOvertime'=>$newOvertimePrev,
                'ActualWorkHours'=>$newActualWorkHours,
            );
            $this->db->update($this->table_name, $data);       
        }
        /**
         * function isCheckLeaveRegistrationPaid
         * Allow check Leave registration Is Paid ?
         */    
        function timeUpdateForOvertimeEmployee($employeeID,$Prevdate,$CurrentTime){
            $hourCurrentdate = date('H',strtotime($CurrentTime));
            $muniteCurrentdate = date('i',strtotime($CurrentTime));
            $MutiteUpdate = ($hourCurrentdate)*60 + $muniteCurrentdate;
            $hourUpdate = round($MutiteUpdate/60,2);
            // get ActualWorkHours for Prevdate
            $result = $this->getActualWorkHoursPrevDate($employeeID,$Prevdate);
            $dbActualWorkHours = $result['ActualWorkHours'];
            $dbActualWorkHours = $dbActualWorkHours + $hourUpdate;
            switch($this->checkDatetype($Prevdate)){
                case '3':
                    $this->reUpdateTimeCardOvertime("HolidayLateOvertime",$hourUpdate,$employeeID,$Prevdate,$dbActualWorkHours); 
                break;
                case '2':
                    $this->reUpdateTimeCardOvertime("WeekendLateOvertime",$hourUpdate,$employeeID,$Prevdate,$dbActualWorkHours); 
                break;
                case '1':
                    $this->reUpdateTimeCardOvertime("WeekdayLateOvertime",$hourUpdate,$employeeID,$Prevdate,$dbActualWorkHours);
                break;
            }       
        }
        /**
         * Function caculate Hour working weekday normal time
         * @param $Arrivaltime and $Leavetime
         * @return hour working weekday late overtime
         */
        function weekdayNormalTime($Arrivaltime,$Leavetime,$EndTime,$StartTime,$EndRestTime1,$StartRestTime1) {
            // case 0
            if(strtotime($Arrivaltime) < strtotime($EndTime)){
                // case 01 $Arrivaltime >= $EndRestTime1
                if(strtotime($Arrivaltime) >= strtotime($EndRestTime1)){
                    // case 001
                    if(strtotime($Leavetime) >= strtotime($EndTime) ){
                        // caculate hour
                        $Hour = $this->hourOneInput($EndTime,$Arrivaltime);
            		return $Hour;                     
                    }
                    else{
                        // caculate hour
                        $Hour = $this->hourOneInput($Leavetime,$Arrivaltime);
            		return $Hour;                    
                    }
                }
                // case 01 $StartRestTime1 <= $Arrivaltime < $EndRestTime1
                if(strtotime($Arrivaltime) >= strtotime($StartRestTime1) && strtotime($Arrivaltime) < strtotime($EndRestTime1)){
                    // case 011
                    if(strtotime($Leavetime) <= strtotime($EndRestTime1)){
                        return $Hour = 0;
                    }
                    else{
                        if(strtotime($Leavetime) >= strtotime($EndTime)){
                            $Hour = $this->hourOneInput($EndTime,$EndRestTime1);
                            return $Hour; 																		
                        }
                        else{
                            // caculate hour
                            $Hour = $this->hourOneInput($Leavetime,$EndRestTime1);
                            return $Hour; 						
                        }                    
                    }
                }
                // case 02 $Arrivaltime < $StartRestTime1 
                if(strtotime($Arrivaltime) < strtotime($StartRestTime1)){
                    // case 021 $Leavetime >= $EndRestTime1 
                    if(strtotime($Leavetime) >=  strtotime($EndTime)){
                        $Hour = $this->hourTwoInput($EndTime, $EndRestTime1, $StartRestTime1, $Arrivaltime);
                        return $Hour;                         
                    }
                    // case 022 $EndRestTime1 <= $Leavetime < $EndTime 
                    if(strtotime($EndRestTime1) <= strtotime($Leavetime) && strtotime($Leavetime) < strtotime($EndTime)){
                        $Hour = $this->hourTwoInput($Leavetime, $EndRestTime1, $StartRestTime1, $Arrivaltime);
                        return $Hour; 
                    }
                    
                    // case 023 $Leavetime < $StartRestTime1 
                    if(strtotime($Leavetime) <= strtotime($StartRestTime1)){
                        // caculate hour
                        $Hour = $this->hourOneInput($Leavetime,$StartTime);
                        return $Hour;                  
                    }
                                    
                }
            }
            // case 1
            else{
                return $Hour = 0;
            }
        }
    /**
     * Function caculate Hour working weekday overtime
     * @param $Arrivaltime and $Leavetime
     * @return hour working weekday late overtime
     */
    function weekdayOvertime($Date,$Arrivaltime,$Leavetime,$EndEveningOvertime,$RestTime2,$shiftEndTime,$overTimeCurrentDate) {
        $Hour = 0;
        // the news time
        $muniteNew = date('i',strtotime($shiftEndTime))+$RestTime2;
        if(($muniteNew%60)==0){
                $numberMunite = "00";	
        }
        else{
                $numberMunite = $muniteNew%60;
        }        
        $newRestime2 = (date('H',strtotime($shiftEndTime)) + intval($muniteNew/60)).":".($numberMunite).":00";
        // total munite Leavetime
        if(date('H',strtotime($Leavetime))=="00"){
                $Leavetime = "23:59:00";
        }         
        if(strtotime($Leavetime)>strtotime($newRestime2)){
            if(strtotime($Leavetime) >= strtotime($EndEveningOvertime) ){
                if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime'])<=strtotime($Date." ".$newRestime2) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime'])>=strtotime($Date." ".$EndEveningOvertime) ) ){
                     $Hour = $this->hourOneInput($EndEveningOvertime,$newRestime2);                
                }
                if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) > strtotime($Date." ".$newRestime2) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) < strtotime($Date." ".$EndEveningOvertime) ) ){
                     $Hour = $this->hourOneInput($overTimeCurrentDate['EndTime'],$overTimeCurrentDate['StartTime']);            
                }
                if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) <= strtotime($Date." ".$newRestime2) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) < strtotime($Date." ".$EndEveningOvertime) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) > strtotime($Date." ".$newRestime2) ) ){
                     $Hour = $this->hourOneInput($overTimeCurrentDate['EndTime'],$newRestime2);           
                }
                if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) > strtotime($Date." ".$newRestime2) ) && ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) < strtotime($Date." ".$EndEveningOvertime) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) > strtotime($Date." ".$EndEveningOvertime) ) ){
                     $Hour = $this->hourOneInput($EndEveningOvertime,$overTimeCurrentDate['StartTime']);            
                }                 
            }
            else{
                if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime'])<=strtotime($Date." ".$newRestime2) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime'])>=strtotime($Date." ".$Leavetime) ) ){
                     $Hour = $this->hourOneInput($Leavetime,$newRestime2);                
                }
                if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) > strtotime($Date." ".$newRestime2) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) < strtotime($Date." ".$Leavetime) ) ){
                     $Hour = $this->hourOneInput($overTimeCurrentDate['EndTime'],$overTimeCurrentDate['StartTime']);            
                }
                if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) <= strtotime($Date." ".$newRestime2) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) < strtotime($Date." ".$Leavetime) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) > strtotime($Date." ".$newRestime2) ) ){
                     $Hour = $this->hourOneInput($overTimeCurrentDate['EndTime'],$newRestime2);           
                }
                if(strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) > strtotime($Date." ".$newRestime2) && strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) < strtotime($Date." ".$Leavetime) && strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) > strtotime($Date." ".$Leavetime)){
                     $Hour = $this->hourOneInput($Leavetime,$overTimeCurrentDate['StartTime']);            
                }                
            }          
          
        }
        return $Hour;
    }
    
    /**
     * Function caculate Hour working weekday late overtime
     * @param $Arrivaltime and $Leavetime
     * @return hour working weekday late overtime
     */
    function weekdayLateOvertime($Date,$Arrivaltime,$Leavetime,$EndEveningOvertime,$RestTime3,$overTimeCurrentDate) {
        $Hour = 0;
        // the news time
        $muniteNew = date('i',strtotime($EndEveningOvertime))+$RestTime3;
        if(($muniteNew%60)==0){
                $numberMunite = "00";	
        }
        else{
                $numberMunite = $muniteNew%60;
        }
        // news point restime 3
        $newRestime3 = (date('H',strtotime($EndEveningOvertime)) + intval($muniteNew/60)).":".($numberMunite).":00";
        // total munite Leavetime
        if(date('H',strtotime($Leavetime))=="00"){
                $Leavetime = "23:59:00";
        }     
        // check leave time > EndEveningOvertime + restime 3
       if(strtotime($Leavetime)>strtotime($newRestime3)){
           // case 1  Start OverTime <= newRestTime3 && End OverTime > Leavetime
           if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime'])<=strtotime($Date." ".$newRestime3) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime'])>=strtotime($Date." ".$Leavetime) ) ){
                $Hour = $this->hourOneInput($Leavetime,$newRestime3);
           }
           // case 2 Start OverTime > newRestTime3 && End OverTime < Leavetime
           if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) > strtotime($Date." ".$newRestime3) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) < strtotime($Date." ".$Leavetime) ) ){
                $Hour = $this->hourOneInput($overTimeCurrentDate['EndTime'],$overTimeCurrentDate['StartTime']);
           }
           // case 3 Start OverTime <= newRestTime3 && End OverTime < Leavetime && End OverTime > newRestTime3
           if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) <= strtotime($Date." ".$newRestime3) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) < strtotime($Date." ".$Leavetime) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) >= strtotime($Date." ".$newRestime3) ) ){
                $Hour = $this->hourOneInput($overTimeCurrentDate['EndTime'],$newRestime3);
           }
           // case 4 Start OverTime > newRestTime3 && Start OverTime < Leavetime && End OverTime > Leavetime
           if( ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) > strtotime($Date." ".$newRestime3) ) && ( strtotime($overTimeCurrentDate['StartDate']." ".$overTimeCurrentDate['StartTime']) <= strtotime($Date." ".$Leavetime) ) && ( strtotime($overTimeCurrentDate['EndDate']." ".$overTimeCurrentDate['EndTime']) >= strtotime($Date." ".$Leavetime) ) ){
                $Hour = $this->hourOneInput($Leavetime,$overTimeCurrentDate['StartTime']);
           }
       }
       return $Hour; 
    }    
    /**
     * Function resTime31 allow caculate restime 3 default
     * @param time $time name to declare
     * @return trimmed time.
     */
    function resTime31($RestTime3) {
        return round($RestTime3/60,2);
    }  
    
    /**
     * Function resTime21 allow caculate restime 2 default
     * @param $Arrivaltime,$Leavetime,$StartRestTime2,$EndRestTime2 to declare
     * @return return hour ResTime2 default
     */
    function resTime21($RestTime2) {
        return round($RestTime2/60,2);
    } 
      
    /**
     * Function resTime11 allow caculate restime 1 default
     * @param $Arrivaltime,$Leavetime,$StartRestTime1,$EndRestTime1 to declare
     * @return return hour ResTime1 default
     */
    function resTime11($Arrivaltime,$Leavetime,$StartRestTime1,$EndRestTime1) {
        $Hour = 0;
        if(strtotime($Arrivaltime) < strtotime($EndRestTime1) ){
            if(strtotime($Arrivaltime) <= strtotime($StartRestTime1) ){
                if( strtotime($Leavetime) > strtotime($EndRestTime1) ){
                    // caculate restime 1 default 
                    $Hour = $this->hourOneInput($EndRestTime1,$StartRestTime1);                     
                }
            }
            else{
                if( strtotime($Leavetime) > strtotime($EndRestTime1) ){
                    // caculate restime 1 default
                    $Hour = $this->hourOneInput($EndRestTime1,$Arrivaltime);                  
                }           
            }
        }
        return $Hour;
    }
    /**
     * Function resTime30 allow caculate restime 3 employee leave registration
     * @param  to declare
     * @return return hour ResTime3 employee leave registration
     */
    function resTime30($Arrivaltime,$Leavetime,$EndEveningOvertime,$leaveStartDate,$leaveEndDate,$RestTime3) {
        $Hour = 0;
        // the news time
        $muniteNew = date('i',strtotime($EndEveningOvertime))+$RestTime3;
        if(($muniteNew%60)==0){
                $numberMunite = "00";	
        }
        else{
                $numberMunite = $muniteNew%60;
        }
        // news point restime 3
        $newRestime3 = (date('H',strtotime($EndEveningOvertime)) + intval($muniteNew/60)).":".($numberMunite).":00";
        if(date('H',strtotime($Leavetime))=="00"){
                $Leavetime = "23:59:00";
        } 
        if(strtotime($Leavetime) > strtotime($newRestime3) ){
            if( ( strtotime($leaveEndDate) > strtotime($newRestime3) ) && ( strtotime($leaveEndDate) < strtotime($Leavetime) ) ){
                if(strtotime($leaveStartDate) < strtotime($newRestime3) ){
                    $Hour1 = $this->hourOneInput($leaveEndDate,$EndEveningOvertime);
                    $muniteRestTime3 = ($Hour1)*60 - $RestTime3;
                    $Hour = round($muniteRestTime3/60,2);                    
                }
                else{
                    $Hour = $this->hourOneInput($leaveEndDate,$leaveStartDate);              
                }
            }
        }
        return $Hour;
    }    
    /**
     * Function resTime20 allow caculate restime 2 employee leave registration
     * @param  to declare
     * @return return hour ResTime2 employee leave registration
     */
    function resTime20($Arrivaltime,$Leavetime,$shiftEndTime,$RestTime2,$EndEveningOvertime,$leaveStartTime,$leaveEndTime) {
        $Hour = 0;
        // the news time
        $muniteNew = date('i',strtotime($shiftEndTime))+$RestTime2;
        if(($muniteNew%60)==0){
                $numberMunite = "00";	
        }
        else{
                $numberMunite = $muniteNew%60;
        }        
        $StartEveningOvertime = (date('H',strtotime($shiftEndTime)) + intval($muniteNew/60)).":".($numberMunite).":00";
        if(date('H',strtotime($Leavetime))=="00"){
                $Leavetime = "23:59:00";
        }        
        if(strtotime($Leavetime) > strtotime($StartEveningOvertime) ){
            if( (strtotime($leaveEndTime) > strtotime($StartEveningOvertime) ) && ( strtotime($leaveEndTime) <= strtotime($Leavetime) ) ){
                // case 1
                if(strtotime($leaveEndTime) >= strtotime($EndEveningOvertime) ){
                    // case 10
                    if(strtotime($leaveStartTime) <= strtotime($StartEveningOvertime) ){
                        $Hour = $this->hourOneInput($EndEveningOvertime,$StartEveningOvertime);                      
                    }
                    else{
                        if(strtotime($leaveStartTime) < strtotime($EndEveningOvertime) ){
                            $Hour = $this->hourOneInput($EndEveningOvertime,$leaveStartTime);   							
                        }
                    }
                }
                else{
                    // case 10
                    if(strtotime($leaveStartTime) <= strtotime($StartEveningOvertime) ){
                        $Hour = $this->hourOneInput($leaveEndTime,$StartEveningOvertime);                       
                    }
                    else{
                        $Hour = $this->hourOneInput($leaveEndTime,$leaveStartTime);                       
                    }                    
                }
            }
        }
        return $Hour;
    }   
    /**
     * Function resTime10 allow caculate restime 1 employee leave registration
     * @param  to declare
     * @return return hour ResTime1 employee leave registration
     */
    function resTime10($Arrivaltime,$Leavetime,$StartRestTime1,$EndRestTime1,$StartTime,$EndTime,$leaveStartTime,$leaveEndTime) {
        $Hour = 0;
        if(date('H',strtotime($Leavetime))=="00"){
                $Leavetime = "23:59:00";
        }        
        if(strtotime($leaveEndTime) <= strtotime($EndTime) ){
            // case 0
            if(strtotime($leaveEndTime) > strtotime($EndRestTime1) ){
                // case 00
                if(strtotime($leaveStartTime) >= strtotime($EndRestTime1) ){
                    // caculate restime 1 leave no paid
                    $Hour = $this->hourOneInput($leaveEndTime,$leaveStartTime);                  
                }
                // case 01
                if(strtotime($leaveStartTime) >= strtotime($StartRestTime1) && strtotime($leaveStartTime) < strtotime($EndRestTime1) ){
                    // caculate restime 1 leave no paid
                    $Hour = $this->hourOneInput($leaveEndTime,$EndRestTime1);                    
                }
                // case 02
                if(strtotime($leaveStartTime) < strtotime($StartRestTime1) ){
                    // caculate restime 1 leave no paid
                    $Hour = $this->hourTwoInput($leaveEndTime, $EndRestTime1, $StartRestTime1, $leaveStartTime);
                }
            }
            else{
                // case 11
                if(strtotime($leaveEndTime) >= strtotime($StartRestTime1) ){
                    // caculate restime 1 leave no paid
                    $Hour = $this->hourOneInput($StartRestTime1,$leaveStartTime);
                }
                else{
                    // caculate restime 1 leave no paid
                    $Hour = $this->hourOneInput($leaveEndTime,$leaveStartTime);                   
                }
            }
        }
        return $Hour;
    }              
    /**
     * Function weekendHalfDayNormalTime allow caculate Normal time employee working week no overtime
     * @param  $ArrivaltimeWeekend,$Leavetime,$HalfDayStartTime,$HalfDayEndTime to declare
     * @return return hour $hourWeekend employee
     */    
    function weekendHalfDayNormalTime($ArrivaltimeWeekend,$Leavetime,$HalfDayStartTime,$HalfDayEndTime){
        // case 1
        if(strtotime($ArrivaltimeWeekend) < strtotime($HalfDayEndTime) ){
            // case 11
            if(strtotime($Leavetime) >= strtotime($HalfDayEndTime) ){
                if(strtotime($ArrivaltimeWeekend) > strtotime($HalfDayStartTime) ){
                    // caculate $hourWeekend
            		$hourHalfDayEndTime = date('H',strtotime($HalfDayEndTime));
            		$muniteHalfDayEndTime = date('i',strtotime($HalfDayEndTime));
                    
            		$hourArrivaltimeWeekend = date('H',strtotime($ArrivaltimeWeekend));
            		$muniteArrivaltimeWeekend = date('i',strtotime($ArrivaltimeWeekend));
                    
                    $Mweekend = ($hourHalfDayEndTime - $hourArrivaltimeWeekend)*60 + ($muniteHalfDayEndTime - $muniteArrivaltimeWeekend);
                    $hourWeekend = round($Mweekend/60,2);
                    return $hourWeekend;   
                }
                else{
                    // caculate $hourWeekend
            		$hourHalfDayEndTime = date('H',strtotime($HalfDayEndTime));
            		$muniteHalfDayEndTime = date('i',strtotime($HalfDayEndTime));
                    
            		$hourHalfDayStartTime = date('H',strtotime($HalfDayStartTime));
            		$muniteHalfDayStartTime = date('i',strtotime($HalfDayStartTime));
                    
                    $Mweekend = ($hourHalfDayEndTime - $hourHalfDayStartTime)*60 + ($muniteHalfDayEndTime - $muniteHalfDayStartTime);
                    $hourWeekend = round($Mweekend/60,2);
                    return $hourWeekend;                    
                }            
            }
            // case 12
            else{
                if(strtotime($ArrivaltimeWeekend) > strtotime($HalfDayStartTime) ){
                    // caculate $hourWeekend
            		$hourLeavetime = date('H',strtotime($Leavetime));
            		$muniteLeavetime = date('i',strtotime($Leavetime));
                    
            		$hourArrivaltimeWeekend = date('H',strtotime($ArrivaltimeWeekend));
            		$muniteArrivaltimeWeekend = date('i',strtotime($ArrivaltimeWeekend));
                    
                    $Mweekend = ($hourLeavetime - $hourArrivaltimeWeekend)*60 + ($muniteLeavetime - $muniteArrivaltimeWeekend);
                    $hourWeekend = round($Mweekend/60,2);
                    return $hourWeekend;   
                }
                else{
                    // caculate $hourWeekend
            		$hourLeavetime = date('H',strtotime($Leavetime));
            		$muniteLeavetime = date('i',strtotime($Leavetime));
                    
            		$hourHalfDayStartTime = date('H',strtotime($HalfDayStartTime));
            		$muniteHalfDayStartTime = date('i',strtotime($HalfDayStartTime));
                    
                    $Mweekend = ($hourLeavetime - $hourHalfDayStartTime)*60 + ($muniteLeavetime - $muniteHalfDayStartTime);
                    $hourWeekend = round($Mweekend/60,2);
                    return $hourWeekend;                    
                } 
            }            
        }
        // case 2
        else{
            return $hourWeekend = 0;
        }       
    }
    /**
     * Function resTimeWeekend11 allow caculate Normal time employee working week no overtime
     * @param  $ArrivaltimeWeekend,$Leavetime,$HalfDayStartTime,$HalfDayEndTime to declare
     * @return return hour resTimeWeekend employee
     */    
    function resTimeWeekend11($ArrivaltimeWeekend,$Leavetime,$HalfDayStartTime,$HalfDayEndTime,$StartTime,$EndTime){
        // case 1
        if(strtotime($Leavetime) > strtotime($HalfDayStartTime) ){
            // case 11
            if(strtotime($EndTime) > strtotime($HalfDayStartTime) && strtotime($EndTime) <= strtotime($Leavetime)  ){
                // case 111
                if(strtotime($EndTime) >= strtotime($HalfDayEndTime) ){
                    // case1111
                    if(strtotime($StartTime) > strtotime($HalfDayStartTime) ){
                        // caculate
                		$hourHalfDayEndTime = date('H',strtotime($HalfDayEndTime));
                		$muniteHalfDayEndTime = date('i',strtotime($HalfDayEndTime));
                        
                		$hourStartTime = date('H',strtotime($StartTime));
                		$muniteStartTime = date('i',strtotime($StartTime));
                        
                        $MresTimeWeekend = ($hourHalfDayEndTime - $hourStartTime)*60 + ($muniteHalfDayEndTime - $muniteStartTime);
                        $hourresTimeWeekend = round($MresTimeWeekend/60,2);
                        return $hourresTimeWeekend;                         
                    }
                    // case1112
                    else{
                        // caculate
                		$hourHalfDayEndTime = date('H',strtotime($HalfDayEndTime));
                		$muniteHalfDayEndTime = date('i',strtotime($HalfDayEndTime));
                        
                		$hourHalfDayStartTime = date('H',strtotime($HalfDayStartTime));
                		$muniteHalfDayStartTime = date('i',strtotime($HalfDayStartTime));
                        
                        $MresTimeWeekend = ($hourHalfDayEndTime - $hourHalfDayStartTime)*60 + ($muniteHalfDayEndTime - $muniteHalfDayStartTime);
                        $hourresTimeWeekend = round($MresTimeWeekend/60,2);
                        return $hourresTimeWeekend; 
                    }
                }
                else{
                    // case1111
                    if(strtotime($StartTime) > strtotime($HalfDayStartTime) ){
                        // caculate
                		$hourEndTime = date('H',strtotime($EndTime));
                		$muniteEndTime = date('i',strtotime($EndTime));
                        
                		$hourStartTime = date('H',strtotime($StartTime));
                		$muniteStartTime = date('i',strtotime($StartTime));
                        
                        $MresTimeWeekend = ($hourEndTime - $hourStartTime)*60 + ($muniteEndTime - $muniteStartTime);
                        $hourresTimeWeekend = round($MresTimeWeekend/60,2);
                        return $hourresTimeWeekend;                         
                    }
                    // case1112
                    else{
                        // caculate
                		$hourEndTime = date('H',strtotime($EndTime));
                		$muniteEndTime = date('i',strtotime($EndTime));
                        
                		$hourHalfDayStartTime = date('H',strtotime($HalfDayStartTime));
                		$muniteHalfDayStartTime = date('i',strtotime($HalfDayStartTime));
                        
                        $MresTimeWeekend = ($hourEndTime - $hourHalfDayStartTime)*60 + ($muniteEndTime - $muniteHalfDayStartTime);
                        $hourresTimeWeekend = round($MresTimeWeekend/60,2);
                        return $hourresTimeWeekend; 
                    }                    
                }
            }
            else{
                return $resTimeWeekend = 0;
            }
        }
        // case 2
        else{
            return $resTimeWeekend = 0;
        }
    }
    /**
     * Trimmed Leave Time
     * @param time $time name to declare
     * @return trimmed time.
     */
    public function trimmedLeavetime($time,$blockTime) {
        $date = strtotime($time);
        // get hour
        $hours = date('H', $date);
        // get minute
        $minute = date('i', $date);
        // block time
        $block = $blockTime;
        // return time
        $time ="";
        if ($minute <= $block ) {
            $H_time = $hours;
            $time = $H_time.":00:00";
        }
        else{
            $H_time = $hours;
            $M_time = $block;
            $time = $H_time.":".$M_time.":00";
        }
        return $time;
    }
    /**
     * Trimmed Arrival Time
     * @param time $time name to declare
     * @return trimmed time.
     */
    public function trimmedArrivaltime($timeInput,$blockTime,$StartTime) {
        $date = strtotime($timeInput);
        // get hour
        $hours = date('H', $date);
        // get minute
        $minute = date('i', $date);
        // block time
        $block = $blockTime;
        // get start time
        $startTime = $StartTime;
        // get hour Start Time
        $HstartTime = date('H', strtotime($startTime));
        // get minute Start Time
        $MstartTime = date('i', strtotime($startTime));
        // return time
        $time ="";
        // case Hour time <= hour start time
        if ($hours <= $HstartTime) {
            if (strtotime($timeInput) <= strtotime($startTime)) {
                $time = $startTime;
            }
            else{
                if($minute<=$block){
                    $H_time = $hours;
                    $M_time = $block;
                    $time = $H_time.":".$M_time.":00";
                }
                else{
                    $xMunite = $minute+$block;
                    if($xMunite<60){
                        $H_time = $hours;
                        $M_time = $block*floor($xMunite/$block);
                        $time = $H_time.":".$M_time.":00";
                    }
                    else{
                        $H_time = $hours + floor($xMunite/60);
                        $time = $H_time.":00:00";
                    }
                }
            }
        }
        // case Hour time > hour start time
        else {
            if($minute<=$block){
                $H_time = $hours;
                $M_time = $block;
                $time = $H_time.":".$M_time.":00";
            }
            else{
                $xMunite = $minute+$block;
                if($xMunite<60){
                    $H_time = $hours;
                    $M_time = $block*floor($xMunite/$block);
                    $time = $H_time.":".$M_time.":00";
                }
                else{
                    $H_time = $hours + floor($xMunite/60);
                    if($H_time<24)
                    {
                        $time = $H_time.":00:00";
                    }
                    else{
                        $time = "24:00:00";
                    }
                }
            }
        }
        return $time;
    } 
    /**
     * Trimmed Arrival time Weekend
     * @param time $time name to declare
     * @return trimmed time.
     */
    public function trimmedArrivaltimeWeekend($timeInput,$blockTime,$HalfDayStartTime) {
        $date = strtotime($timeInput);
        // get hour
        $hours = date('H', $date);
        // get minute
        $minute = date('i', $date);
        // block time
        $block = $blockTime;
        // get start time
        $startTime = $HalfDayStartTime;
        // get hour Start Time
        $HstartTime = date('H', strtotime($HalfDayStartTime));
        // get minute Start Time
        $MstartTime = date('i', strtotime($HalfDayStartTime));
        // return time
        $time ="";
        // case Hour time <= hour start time
        if ($hours <= $HstartTime) {
            if (strtotime($time) <= strtotime($startTime)) {
                $time = $startTime;
            }
            else{
                if($minute<=$block){
                    $H_time = $hours;
                    $M_time = $block;
                    $time = $H_time.":".$M_time.":00";
                }
                else{
                    $xMunite = $minute+$block;
                    if($xMunite<60){
                        $H_time = $hours;
                        $M_time = $block*floor($xMunite/$block);
                        $time = $H_time.":".$M_time.":00";
                    }
                    else{
                        $H_time = $hours + floor($xMunite/60);
                        $time = $H_time.":00:00";
                    }
                }
            }
        }
        // case Hour time > hour start time
        else {
            if($minute<=$block){
                $H_time = $hours;
                $M_time = $block;
                $time = $H_time.":".$M_time.":00";
            }
            else{
                $xMunite = $minute+$block;
                if($xMunite<60){
                    $H_time = $hours;
                    $M_time = $block*floor($xMunite/$block);
                    $time = $H_time.":".$M_time.":00";
                }
                else{
                    $H_time = $hours + floor($xMunite/60);
                    if($H_time<24)
                    {
                        $time = $H_time.":00:00";
                    }
                    else{
                        $time = "24:00:00";
                    }
                }
            }
        }
        return $time;
    }    
    /**
     * function isCheckLeaveRegistrationPaid
     * Allow check Leave registration Is Paid ?
     */
    function isCheckLeaveRegistrationPaid($LeaveTypeId,$employeeID,$date){
        $leaveId = $this->isLeaveRegistrationPaid($LeaveTypeId,$employeeID,$date);
        if(isset($leaveId) && $leaveId['IsPaidLeave'] == 1){
            return true;
        }
        else{
            return false;
        }
    } 
    /**
     * function isLeaveRegistrationPaid
     * check type
     */ 
    function isLeaveRegistrationPaid($LeaveTypeId,$employeeID,$date){
        $this->db->join("m_leavetype","m_leaveregistration.LeaveTypeId = m_leavetype.LeaveTypeId");
        $this->db->select("m_leavetype.IsPaidLeave");
        $this->db->where('m_leaveregistration.EmployeeId',$employeeID);
        $this->db->where("m_leaveregistration.LeaveTypeId",$LeaveTypeId);
        $this->db->where('m_leaveregistration.StartDate <= ', $date);
        $this->db->where('m_leaveregistration.EndDate >= ', $date);            
        $query = $this->db->get($this->table_leaveregistration);
        return $query->row_array();
    }    
    /**
     * function checkDatetype
     * Allow check date is holiday or weekend or normal day
     */
    function checkDatetype($date){
        $day = date("w", strtotime($date));
        // check this date is holiday ?
        if($this->countHolidayDate($date)>0){
            return 3;
        }
        // check date weekend
        if($day==0||$day==6){
            return 2;
        }
        else{
            return 1;
        }
    }  
    function countHolidayDate($date){
        $newDate = date("y-m-d", strtotime($date));
        $this->db->where('Date',$newDate);
        $this->db->where('CompanyCode',$this->session->userdata('CompanyCode'));
        $this->db->where('Del_Flag',0);
        $query = $this->db->get($this->table_holiday);
        return $query->num_rows();
    }    
    function array_orderby()
    {
        $args = func_get_args();
        $data = array_shift($args);
        foreach ($args as $n => $field) {
            if (is_string($field)) {
                $tmp = array();
                foreach ($data as $key => $row)
                    $tmp[$key] = $row[$field];
                $args[$n] = $tmp;
                }
        }
        $args[] = &$data;
        call_user_func_array('array_multisort', $args);
        return array_pop($args);
    }                            
    /**
     * function getTimeLeaveRegistrationIsPaid
     * Allow caculation time is paid
     */
    function getTimeLeaveRegistrationIsPaid($Arrivaltime,$Leavetime,$registrationDates,$TotalWorkHours,$StartTimeAfternoon,$employeeID,$date,$StartRestTime1,$EndRestTime1,$endTimeNormalWorking){
        // caculate
        $flag = 0;
        $StartTime = "";
        $EndTime = "";
        $moreTime = array();
        foreach($registrationDates as $leaveRegistration){
            if($this->isCheckLeaveRegistrationPaid($leaveRegistration['LeaveTypeId'],$employeeID,$date)){
                $flag = 1;
                $StartTime = $leaveRegistration['StartTime'];
                $EndTime = $leaveRegistration['EndTime'];
                break;
            }
        }
        if($flag==1){
            // $StartTimeAfternoon = $EndRestTime1
            if(strtotime($StartTimeAfternoon) == strtotime($EndRestTime1)){
                $moreTime['addtime'] = $TotalWorkHours/2;
                $moreTime['downtime'] = 0;
            }
            // $StartTimeAfternoon > $EndRestTime1
            else{
                // morning leave
                if(strtotime($StartTime) < strtotime($StartRestTime1) ){
                    // time need reset
                    if(strtotime($Arrivaltime) <= strtotime($StartTimeAfternoon)){
                        if(strtotime($Arrivaltime) >= strtotime($EndRestTime1)){
                    		$hourStartTimeAfternoon = date('H',strtotime($StartTimeAfternoon));
                    		$muniteStartTimeAfternoon = date('i',strtotime($StartTimeAfternoon));
                            
                    		$hourArrivaltime = date('H',strtotime($Arrivaltime));
                    		$muniteArrivaltime = date('i',strtotime($Arrivaltime));
                            
                            $Mtime = ($hourStartTimeAfternoon - $hourArrivaltime)*60 + ($muniteStartTimeAfternoon - $muniteArrivaltime);
                            $hourTime = round($Mtime/60,2);
                            $moreTime['addtime'] = $TotalWorkHours/2;
                            $moreTime['downtime'] = -$hourTime;
                            return $moreTime;                           
                        }
                        else{
                            if(strtotime($Arrivaltime) < strtotime($StartRestTime1)){
                        		$hourStartTimeAfternoon = date('H',strtotime($StartTimeAfternoon));
                        		$muniteStartTimeAfternoon = date('i',strtotime($StartTimeAfternoon));
                                
                        		$hourEndRestTime1 = date('H',strtotime($EndRestTime1));
                        		$muniteEndRestTime1 = date('i',strtotime($EndRestTime1));
                                
                                $Mtime1 = ($hourStartTimeAfternoon - $hourEndRestTime1)*60 + ($muniteStartTimeAfternoon - $muniteEndRestTime1);
                                $hourTime1 = round($Mtime1/60,2);  
     		                    
                                $hourStartRestTime1 = date('H',strtotime($StartRestTime1));
                        		$muniteStartRestTime1 = date('i',strtotime($StartRestTime1));
                                
                        		$hourArrivaltime = date('H',strtotime($Arrivaltime));
                        		$muniteArrivaltime = date('i',strtotime($Arrivaltime));
                                
                                $Mtime2 = ($hourStartRestTime1 - $hourArrivaltime)*60 + ($muniteStartRestTime1 - $muniteArrivaltime);
                                $hourTime2 = round($Mtime2/60,2);
                                
                                $moreTime['addtime'] = $TotalWorkHours/2;
                                $moreTime['downtime'] = -($hourTime1 + $hourTime2);
                                return $moreTime;                                                   
                            }
                            else{
                        		$hourStartTimeAfternoon = date('H',strtotime($StartTimeAfternoon));
                        		$muniteStartTimeAfternoon = date('i',strtotime($StartTimeAfternoon));
                                
                        		$hourEndRestTime1 = date('H',strtotime($EndRestTime1));
                        		$muniteEndRestTime1 = date('i',strtotime($EndRestTime1));
                                
                                $Mtime = ($hourStartTimeAfternoon - $hourEndRestTime1)*60 + ($muniteStartTimeAfternoon - $muniteEndRestTime1);
                                $hourTime = round($Mtime/60,2); 
                                
                                $moreTime['addtime'] = $TotalWorkHours/2;
                                $moreTime['downtime'] = -$hourTime;
                                return $moreTime;                                                                  
                            }
                        }
                    }
                    else{
                        $moreTime['addtime'] = $TotalWorkHours/2;
                        $moreTime['downtime'] = 0;
                        return $moreTime;                         
                    }
                }
                // afternoons leave
                else{
                    if(strtotime($Leavetime) >= strtotime($EndRestTime1)){
                        if(strtotime($Leavetime) >= strtotime($endTimeNormalWorking)){
                           if(strtotime($Arrivaltime) < strtotime($StartRestTime1)){ 
                            
                                $hourTimeNormalWorking = date('H',strtotime($endTimeNormalWorking));
                        		$muniteTimeNormalWorking = date('i',strtotime($endTimeNormalWorking));
                                
                        		$hourEndRestTime1 = date('H',strtotime($EndRestTime1));
                        		$muniteEndRestTime1 = date('i',strtotime($EndRestTime1));
                                
                                $Mtime1 = ($hourTimeNormalWorking - $hourEndRestTime1)*60 + ($muniteTimeNormalWorking - $muniteEndRestTime1);
                                $hourTime1 = round($Mtime1/60,2);
                                
                                
                                $hourEndRestTime1 = date('H',strtotime($EndRestTime1));
                        		$muniteEndRestTime1 = date('i',strtotime($EndRestTime1));
                                
                        		$hourStartRestTime1 = date('H',strtotime($StartRestTime1));
                        		$muniteStartRestTime1 = date('i',strtotime($StartRestTime1));
                                
                                $Mtime2 = ($hourEndRestTime1 - $hourStartRestTime1)*60 + ($muniteEndRestTime1 - $muniteStartRestTime1);
                                $hourTime2 = round($Mtime2/60,2);                                
                                
                                
                                $moreTime['addtime'] = $TotalWorkHours/2;
                                $moreTime['downtime'] =  $hourTime2 - $hourTime1;
                                return $moreTime;                                 
                           }
                           else{
                                if(strtotime($Arrivaltime) >= strtotime($EndRestTime1)&& strtotime($Arrivaltime) <= strtotime($endTimeNormalWorking) ){ 
                                    $hourendTimeNormalWorking = date('H',strtotime($endTimeNormalWorking));
                            		$muniteendTimeNormalWorking = date('i',strtotime($endTimeNormalWorking));
                                    
                            		$hourArrivaltime = date('H',strtotime($Arrivaltime));
                            		$muniteArrivaltime = date('i',strtotime($Arrivaltime));
                                    
                                    $Mtime = ($hourendTimeNormalWorking - $hourArrivaltime)*60 + ($muniteendTimeNormalWorking - $muniteArrivaltime);
                                    $hourTime = round($Mtime2/60,2);                                
                                    
                                    
                                    $moreTime['addtime'] = $TotalWorkHours/2;
                                    $moreTime['downtime'] =  -$hourTime1;
                                    return $moreTime;                                      
                                }
                                else{
                                    $moreTime['addtime'] = $TotalWorkHours/2;
                                    $moreTime['downtime'] =  0;
                                    return $moreTime;                                     
                                }
                           }
                        }
                        else{
                           if(strtotime($Arrivaltime) < strtotime($StartRestTime1)){ 
                            
                                $hourLeavetime = date('H',strtotime($Leavetime));
                        		$muniteLeavetime = date('i',strtotime($Leavetime));
                                
                        		$hourEndRestTime1 = date('H',strtotime($EndRestTime1));
                        		$muniteEndRestTime1 = date('i',strtotime($EndRestTime1));
                                
                                $Mtime1 = ($hourLeavetime - $hourEndRestTime1)*60 + ($muniteLeavetime - $muniteEndRestTime1);
                                $hourTime1 = round($Mtime1/60,2);
                                
                                
                                $hourEndRestTime1 = date('H',strtotime($EndRestTime1));
                        		$muniteEndRestTime1 = date('i',strtotime($EndRestTime1));
                                
                        		$hourStartRestTime1 = date('H',strtotime($StartRestTime1));
                        		$muniteStartRestTime1 = date('i',strtotime($StartRestTime1));
                                
                                $Mtime2 = ($hourEndRestTime1 - $hourStartRestTime1)*60 + ($muniteEndRestTime1 - $muniteStartRestTime1);
                                $hourTime2 = round($Mtime2/60,2);                                
                                
                                
                                $moreTime['addtime'] = $TotalWorkHours/2;
                                $moreTime['downtime'] =  $hourTime2 - $hourTime1;
                                return $moreTime;                                 
                           }
                           else{
                                if(strtotime($Arrivaltime) >= strtotime($EndRestTime1)){ 
                                    $hourendLeavetime = date('H',strtotime($Leavetime));
                            		$muniteendLeavetime = date('i',strtotime($Leavetime));
                                    
                            		$hourArrivaltime = date('H',strtotime($Arrivaltime));
                            		$muniteArrivaltime = date('i',strtotime($Arrivaltime));
                                    
                                    $Mtime = ($hourendLeavetime - $hourArrivaltime)*60 + ($muniteendLeavetime - $muniteArrivaltime);
                                    $hourTime = round($Mtime2/60,2);                                
                                    
                                    
                                    $moreTime['addtime'] = $TotalWorkHours/2;
                                    $moreTime['downtime'] =  -$hourTime1;
                                    return $moreTime;                                      
                                }
                                else{
                                    $hourLeavetime = date('H',strtotime($Leavetime));
                            		$muniteLeavetime = date('i',strtotime($Leavetime));
                                    
                            		$hourEndRestTime1 = date('H',strtotime($EndRestTime1));
                            		$muniteEndRestTime1 = date('i',strtotime($EndRestTime1));
                                    
                                    $Mtime = ($hourLeavetime - $hourEndRestTime1)*60 + ($muniteLeavetime - $muniteEndRestTime1);
                                    $hourTime = round($Mtime/60,2);                               
                                    
                                    
                                    $moreTime['addtime'] = $TotalWorkHours/2;
                                    $moreTime['downtime'] =  $hourTime;
                                    return $moreTime;                                      
                                }
                           }                            
                        }
                    }
                    else{
                        if(strtotime($Leavetime) > strtotime($StartRestTime1)){
                            if(strtotime($Arrivaltime) < strtotime($StartRestTime1)){
                                $hourLeavetime = date('H',strtotime($Leavetime));
                        		$muniteLeavetime = date('i',strtotime($Leavetime));
                                
                        		$hourStartRestTime1 = date('H',strtotime($StartRestTime1));
                        		$muniteStartRestTime1 = date('i',strtotime($StartRestTime1));
                                
                                $Mtime = ($hourLeavetime - $hourStartRestTime1)*60 + ($muniteLeavetime - $muniteStartRestTime1);
                                $hourTime = round($Mtime/60,2);
                                
                                $moreTime['addtime'] = $TotalWorkHours/2;
                                $moreTime['downtime'] = $hourTime;
                                return $moreTime;                                 
                            }
                            else{
                                $moreTime['addtime'] = $TotalWorkHours/2;
                                $moreTime['downtime'] = 0;
                                return $moreTime;                                 
                            }
                        }
                        else{
                                $moreTime['addtime'] = $TotalWorkHours/2;
                                $moreTime['downtime'] = 0;
                                return $moreTime;                             
                        }
                    }
                }
            }
        }
        else{
            $moreTime['addtime'] = 0;
            $moreTime['downtime'] = 0 ;
            return $moreTime;
        }
    }                     
    /**
     * function caculation LateArrival
     * Caculation time Late Arrival of employee
     */       
     function caculationLateArrival($Arrivaltime,$StartTime,$ActualArrivalTime){
            $LateArrival = 0;
            // LateArrival
            if(strtotime($Arrivaltime) > strtotime($StartTime) ){
                // caculate
                $hourArrivaltime = date('H',strtotime($ActualArrivalTime));
                $muniteArrivaltime = date('i',strtotime($ActualArrivalTime));

                $hourStartTime = date('H',strtotime($StartTime));
                $muniteStartTime = date('i',strtotime($StartTime));

                $MLateArrival = ($hourArrivaltime - $hourStartTime)*60 + ($muniteArrivaltime - $muniteStartTime);
                $hourresLateArrival = round($MLateArrival/60,2);
                $LateArrival =  $hourresLateArrival;
                return $LateArrival;
            }
            else{
                return $LateArrival = 0;
            }
     }
     
    /**
     * function caculation EarlyLeave
     * Caculation time Early Leave of employee
     */       
     function caculationEarlyLeave($Leavetime,$EndTime,$ActualLeaveTime){
        $EarlyLeave = 0;
        //  EarlyLeave
        if(strtotime($Leavetime) < strtotime($EndTime) ){
            // caculate
            $hourEndTime = date('H',strtotime($EndTime));
            $muniteEndTime = date('i',strtotime($EndTime));

            $hourleavetime = date('H',strtotime($ActualLeaveTime));
            $muniteleavetime = date('i',strtotime($ActualLeaveTime));

            $MEarlyLeave = ($hourEndTime - $hourleavetime)*60 + ($muniteEndTime - $muniteleavetime);
            $hourresEarlyLeave = round($MEarlyLeave/60,2);
            $EarlyLeave =  $hourresEarlyLeave;
            return $EarlyLeave;
        }
        else{
            return $EarlyLeave = 0;
        }
     }
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    get list shiftinfo
     * @Input :         void
     * @Output :        return array data
     */

    function listShiftInfo() {
        $this->db->from($this->table_shiftinfo);
        $this->db->where('CompanyCode',$this->session->userdata('CompanyCode'));
        $this->db->where('Del_Flag',0);
        $this->db->order_by("ShiftId", "ASC"); 
        $query = $this->db->get();
        return $query->row_array(0);;
    }
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    get list all employee data
     * @Input :         void
     * @Output :        return array data employee
     */
    function listAllEmployee() {
        $this->db->from($this->table_employee);
        $this->db->where('CompanyCode',$this->session->userdata('CompanyCode'));
        $this->db->where('Status',1);
        $this->db->where('Del_Flag',0);
        $query = $this->db->get();
        return $query->result_array();
    } 
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    set up info show view
     * @Input :         Null
     * @Output :        return array data
     */    
    function getInfo(){
        $data['title'] = $this->lang->line('tcmanipulation_title');
        $data['ReviewData'] = $this->lang->line('tcmanipulation_ReviewData');
        $data['SelectData'] = $this->lang->line('tcmanipulation_SelectData');
        $data['js'] = array('jquery-ui.js');
        $data['css'] = array('jquery-ui.css');        
        $data['show'] = "index";
        return $data;
    }
    /*
     * @author :        HungLV
     * @created:        
     * @Description:    return hour
     * @Input :         (Time) $endTime,$startTime
     * @Output :        (float) hour
     */
    function hourOneInput($endTime,$startTime){
        $hourEndTime = date('H',strtotime($endTime));
        $muniteEndTime = date('i',strtotime($endTime));

        $hourStartTime = date('H',strtotime($startTime));
        $muniteStartTime = date('i',strtotime($startTime));

        $totalMunite = ($hourEndTime - $hourStartTime)*60 + ($muniteEndTime - $muniteStartTime);
        $hourTime = round($totalMunite/60,2);
        return $hourTime;            
    }
    /*
     * @author :        HungLV
     * @created:        
     * @Description:    return Hour with 2 ranger
     * @Input :         (Pair Time 1) $endTime1,$startTime1 (Pair Time 2) $endTime2,$startTime2
     * @Output :        (float) hour
     */
    function hourTwoInput($endTime1,$startTime1,$endTime2,$startTime2){

        // get hour 1
        $hourEndTime1 = date('H',strtotime($endTime1));
        $muniteEndTime1 = date('i',strtotime($endTime1));

        $hourStartTime1 = date('H',strtotime($startTime1));
        $muniteStartTime1 = date('i',strtotime($startTime1));

        $totalMunite1 = ($hourEndTime1 - $hourStartTime1)*60 + ($muniteEndTime1 - $muniteStartTime1);
        $hourTime1 = round($totalMunite1/60,2);

        // get hour 2
        $hourEndTime2 = date('H',strtotime($endTime2));
        $muniteEndTime2 = date('i',strtotime($endTime2));

        $hourStartTime2 = date('H',strtotime($startTime2));
        $muniteStartTime2 = date('i',strtotime($startTime2));

        $totalMunite2 = ($hourEndTime2 - $hourStartTime2)*60 + ($muniteEndTime2 - $muniteStartTime2);
        $hourTime2 = round($totalMunite2/60,2);

        return $hourTime1+$hourTime2;        
    }    
 }
      
?>